const allServices = require('../../controller/mysql')

const shop = {
    shopList: (req, res) => {
        const { page, pageSize } = req.body;
        const offset = (page - 1) * pageSize;
        const limit = Number(pageSize);
        const sql = "SELECT * FROM shoplist LIMIT ? OFFSET ?";
        const sql1 = "SELECT COUNT(*) AS count FROM shoplist";
        Promise.all([allServices.query(sql, [limit, offset]), allServices.query(sql1)])
            .then(([rows, [{ count }]]) => {
                res.send({
                    code: 200,
                    msg: "请求成功",
                    data: rows,
                    count: count,
                });
            })
            .catch((error) => console.log(error));
    },
    shopDetail: (req, res) => {
        const shop_id = req.query.id
        const sql = `select * from shoplist where shop_id=?`
        allServices.query(sql, [shop_id]).then(result => {
            res.send({
                code: 200,
                msg: "请求成功",
                res: result
            })
        }).catch(err => {
            console.log(err);
        })
    },
    catogory:(req, res) => {
        const { page, pageSize, tag } = req.body;
        const offset = (page - 1) * pageSize;
        const limit = Number(pageSize);
        const sql = "SELECT * FROM shoplist where tag = ? LIMIT ? OFFSET ? ";
        const sql1 = "SELECT COUNT(*) AS count FROM shoplist";
        Promise.all([allServices.query(sql, [tag, limit, offset]), allServices.query(sql1)])
            .then(([rows, [{ count }]]) => {
                res.send({
                    code: 200,
                    msg: "请求成功",
                    res: rows,
                    count: count,
                });
            })
            .catch((error) => console.log(error));
    }
}

module.exports = shop